<template>
  <div class="login-container">
    <!-- 地球背景 -->
    <!-- <EarthBackground></EarthBackground> -->

    <!-- 主要内容 -->
    <div class="login-content">
      <!-- 登录表单 -->
      <div class="login-form-container">
        <div class="login-form-wrapper">
          <div class="login-form">
            <div class="form-header">
              <h2 class="form-title">智能分流系统</h2>
              <p class="form-subtitle">请使用您的账号密码登录系统</p>
            </div>

            <form @submit.prevent="handleLogin" class="form">
              <div class="form-group">
                <label class="form-label">用户名</label>
                <div class="input-wrapper">
                  <input
                    v-model="form.username"
                    type="text"
                    class="form-input"
                    placeholder="请输入用户名"
                    :class="{ 'error': errors.username }"
                    required
                  >
                  <div class="input-icon">👤</div>
                </div>
                <div v-if="errors.username" class="error-message">{{ errors.username }}</div>
              </div>

              <div class="form-group">
                <label class="form-label">密码</label>
                <div class="input-wrapper">
                  <input
                    v-model="form.password"
                    type="password"
                    class="form-input"
                    placeholder="请输入密码"
                    :class="{ 'error': errors.password }"
                    required
                  >
                  <div class="input-icon">🔒</div>
                </div>
                <div v-if="errors.password" class="error-message">{{ errors.password }}</div>
              </div>

              <div class="form-options">
                <label class="checkbox-wrapper">
                  <input v-model="form.remember" type="checkbox" class="checkbox">
                  <span class="checkmark"></span>
                  <span class="checkbox-text">记住我</span>
                </label>
                <a href="#" class="forgot-password">忘记密码？</a>
              </div>

              <button type="submit" class="login-btn" :disabled="loginLoading">
                <span v-if="loginLoading" class="loading-spinner"></span>
                {{ loginLoading ? '登录中...' : '登录' }}
              </button>
            </form>

            <!-- 账号提示 -->
            <div class="account-hint" v-if="false">
              <p class="hint-title">测试账号：</p>
              <div class="hint-items">
                <div class="hint-item">
                  <strong>admin</strong> - 系统管理员 (跳转到首页)
                </div>
                <div class="hint-item">
                  <strong>detention-admin</strong> - 收押分流管理员 (跳转到智能收押分流系统)
                </div>
              </div>
              <p class="hint-note">密码任意填写即可</p>
            </div>

            <div class="form-footer">
              <p>还没有账号？ <a href="#" class="register-link">立即注册</a></p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 成功提示 -->
    <transition name="fade">
      <div v-if="showSuccess" class="success-toast">
        <div class="success-icon">✅</div>
        <div class="success-text">登录成功，正在跳转...</div>
      </div>
    </transition>
  </div>
</template>

<script>
import EarthBackground from '@/components/EarthBackground.vue'

export default {
  name: 'Login',
  components: {
    EarthBackground
  },
  data() {
    return {
      form: {
        username: '',
        password: '',
        remember: false
      },
      errors: {},
      loginLoading: false,
      showSuccess: false
    }
  },
  mounted() {
    // 检查是否已登录
    if (this.$store.getters['user/isAuthenticated']) {
      this.$router.push('/')
    }
  },
  methods: {
    async handleLogin() {
      this.errors = {}

      // 简单验证
      if (!this.form.username.trim()) {
        this.errors.username = '请输入用户名'
        return
      }

      if (!this.form.password.trim()) {
        this.errors.password = '请输入密码'
        return
      }

      this.loginLoading = true

      try {
        // 调用store的登录方法
        await this.$store.dispatch('user/login', {
          username: this.form.username.trim(),
          password: this.form.password.trim()
        })

        // 显示成功提示
        this.showSuccess = true

        // 延迟跳转
        setTimeout(() => {
          // 根据账号类型决定跳转页面
          let redirectPath = '/'

          // 根据用户名决定跳转页面
          if (this.form.username.trim() === 'detention-admin') {
            redirectPath = '/smart-detention'
          } else if (this.form.username.trim() === 'admin') {
            redirectPath = '/'
          }

          // 检查是否有重定向路径，如果有则优先使用（但只在同类型账号内）
          const queryRedirect = this.$route.query.redirect
          if (queryRedirect) {
            // 如果有重定向路径，检查是否与账号类型匹配
            if (this.form.username.trim() === 'detention-admin' && queryRedirect.startsWith('/smart-detention')) {
              redirectPath = queryRedirect
            } else if (this.form.username.trim() === 'admin' && (queryRedirect === '/' || queryRedirect.startsWith('/about'))) {
              redirectPath = queryRedirect
            }
          }

          this.$router.push(redirectPath)
        }, 1500)

      } catch (error) {
        this.errors.general = error.message || '登录失败，请检查用户名和密码'
      } finally {
        this.loginLoading = false
      }
    }
  }
}
</script>

<style scoped>
/* 基础样式 */
.login-container {
  position: relative;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  font-family: 'Microsoft YaHei', 'PingFang SC', sans-serif;
  /* background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%); */
  /* 错误的用法 - CSS loader 不识别 @ 别名 */
  /* background: url('@/assets/images/login-bg.jpeg') no-repeat center center; */

  /* 正确的用法 - 使用 ~@ 前缀让 webpack 解析模块路径 */
  background: url('~@/assets/images/login-bg.jpeg') no-repeat center center;
  background-size: cover;
}

/* 主要内容布局 */
.login-content {
  position: relative;
  z-index: 10;
  display: flex;
  width: 100%;
  max-width: 500px;
  min-height: 350px;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(15px);
  border-radius: 15px;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.1);
  overflow: hidden;
  margin: 20px;
}

/* 左侧信息区域 */
.login-info {
  flex: 1;
  padding: 20px;
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.05), rgba(0, 0, 0, 0.1));
}

.brand {
  margin-bottom: 15px;
}

.brand-title {
  font-size: 1.2rem;
  font-weight: 700;
  margin: 0 0 10px 0;
  background: linear-gradient(45deg, #fff, #e0e7ff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.brand-subtitle {
  font-size: 0.8rem;
  opacity: 0.9;
  margin: 0;
  line-height: 1.4;
}

.features {
  display: grid;
  gap: 10px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.feature-item:hover {
  background: rgba(255, 255, 255, 0.15);
}

.feature-icon {
  font-size: 1.2rem;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 6px;
}

.feature-text h3 {
  margin: 0 0 4px 0;
  font-size: 0.8rem;
  font-weight: 600;
}

.feature-text p {
  margin: 0;
  opacity: 0.8;
  font-size: 0.7rem;
}

/* 登录表单容器 */
.login-form-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 15px;
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(10px);
}

.login-form-wrapper {
  width: 100%;
  max-width: 400px;
}

.login-form {
  width: 100%;
}

.form-header {
  text-align: center;
  margin-bottom: 20px;
}

.form-title {
  font-size: 1.2rem;
  font-weight: 700;
  color: white;
  margin: 0 0 8px 0;
}

.form-subtitle {
  color: rgba(255, 255, 255, 0.7);
  margin: 0;
  font-size: 0.8rem;
}

/* 表单样式 */
.form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-label {
  font-weight: 600;
  color: rgba(255, 255, 255, 0.9);
  font-size: 0.9rem;
}

.input-wrapper {
  position: relative;
}

.form-input {
  width: 100%;
  padding: 10px 35px 10px 15px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  font-size: 0.9rem;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  color: white;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
  transform: translateY(-2px);
  background: rgba(255, 255, 255, 0.1);
}

.form-input.error {
  border-color: #e53e3e;
  box-shadow: 0 0 0 3px rgba(229, 62, 62, 0.2);
}

.form-input::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.input-icon {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 1rem;
  color: rgba(255, 255, 255, 0.6);
  pointer-events: none;
}

.error-message {
  color: #e53e3e;
  font-size: 0.85rem;
  margin-top: 4px;
}

/* 表单选项 */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.checkbox-wrapper {
  display: flex;
  align-items: center;
  cursor: pointer;
  font-size: 0.9rem;
  color: rgba(255, 255, 255, 0.8);
}

.checkbox {
  display: none;
}

.checkmark {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 4px;
  margin-right: 8px;
  position: relative;
  transition: all 0.2s ease;
}

.checkbox:checked + .checkmark {
  background: #667eea;
  border-color: #667eea;
}

.checkbox:checked + .checkmark::after {
  content: '✓';
  position: absolute;
  top: -2px;
  left: 2px;
  color: white;
  font-size: 12px;
  font-weight: bold;
}

.forgot-password {
  color: rgba(255, 255, 255, 0.7);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  transition: color 0.2s ease;
}

.forgot-password:hover {
  color: #667eea;
  text-decoration: underline;
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  padding: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 8px;
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 25px rgba(102, 126, 234, 0.3);
}

.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.loading-spinner {
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 账号提示 */
.account-hint {
  margin-top: 20px;
  padding: 15px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.hint-title {
  margin: 0 0 10px 0;
  color: rgba(255, 255, 255, 0.9);
  font-size: 0.85rem;
  font-weight: 600;
}

.hint-items {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 10px;
}

.hint-item {
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.8rem;
  line-height: 1.4;
}

.hint-item strong {
  color: #667eea;
  font-weight: 700;
}

.hint-note {
  margin: 0;
  color: rgba(255, 255, 255, 0.6);
  font-size: 0.75rem;
  text-align: center;
}

/* 表单底部 */
.form-footer {
  text-align: center;
  margin-top: 15px;
  padding-top: 10px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.form-footer p {
  margin: 0;
  color: rgba(255, 255, 255, 0.6);
  font-size: 0.8rem;
}

.register-link {
  color: rgba(255, 255, 255, 0.7);
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s ease;
}

.register-link:hover {
  color: #667eea;
  text-decoration: underline;
}

/* 成功提示 */
.success-toast {
  position: fixed;
  top: 20px;
  right: 20px;
  background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
  color: white;
  padding: 16px 24px;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(72, 187, 120, 0.3);
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 1000;
  backdrop-filter: blur(10px);
}

.success-icon {
  font-size: 1.5rem;
}

.success-text {
  font-weight: 500;
}

/* 淡入淡出动画 */
.fade-enter-active, .fade-leave-active {
  transition: all 0.3s ease;
}

.fade-enter, .fade-leave-to {
  opacity: 0;
  transform: translateY(-20px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-content {
    margin: 10px;
    min-height: auto;
    max-width: 100%;
  }

  .login-form-container {
    padding: 15px;
  }

  .form-title {
    font-size: 1.1rem;
  }
}
</style>
